package org.jeecg.common.modules.doctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.modules.doctor.entity.Doctor;
import org.jeecg.common.modules.doctor.entity.DoctorAccount;
import org.jeecg.common.modules.doctor.entity.DoctorAccountFlow;
import org.jeecg.common.modules.doctor.mapper.DoctorAccountMapper;
import org.jeecg.common.modules.doctor.service.IDoctorAccountFlowService;
import org.jeecg.common.modules.doctor.service.IDoctorAccountService;
import org.jeecg.common.modules.doctor.vo.DoctorAccountInfoReqVo;
import org.jeecg.common.util.enums.AccountFlowTypeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @Description: doctor_account
 * @Author: jeecg-boot
 * @Date:   2020-08-22
 * @Version: V1.0
 */
@Service
@Slf4j
public class DoctorAccountServiceImpl extends ServiceImpl<DoctorAccountMapper,
        DoctorAccount> implements IDoctorAccountService {

    @Resource
    private IDoctorAccountFlowService doctorAccountFlowService;

    @Resource
    private DoctorAccountMapper doctorAccountMapper;

    @Override
    public List<DoctorAccountInfoReqVo> getDoctorAccountInfos(String name, Integer dataStart, Integer pageSize){
        //传参到mapper类中：selectDoctorAccountInfo
        return doctorAccountMapper.selectDoctorAccountInfo(name,dataStart,pageSize);
    }

    @Override
    public int getDoctorAccountCount(String name){
        return baseMapper.getDoctorAccountCount(name);
    }

    @Override
    public DoctorAccount getOrCreateAccount(Doctor doctor) {
        DoctorAccount exits =getByDoctorId(doctor.getId());
        if (Objects.isNull(exits)){
            exits = new DoctorAccount();
            exits.setDoctorId(doctor.getId());
            exits.setReservedPhone(doctor.getMobile());
            exits.setBalance(BigDecimal.ZERO);
            exits.setTotalAmount(BigDecimal.ZERO);
            save(exits);
        }
        return exits;
    }

    @Override
    public DoctorAccount getByDoctorId(String doctorId){
        LambdaQueryWrapper<DoctorAccount> query = Wrappers.lambdaQuery();
        query.eq(DoctorAccount::getDoctorId,doctorId);
        return getOne(query);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public DoctorAccountFlow changeBalance(String doctorId, BigDecimal amount, AccountFlowTypeEnum flowType, String relationId){
        DoctorAccount doctorAccount = getByDoctorId(doctorId);
        if (Objects.isNull(doctorAccount)){
            throw new RuntimeException("未获取到医生账户信息");
        }
        BigDecimal changeTotalAmount = Objects.equals(flowType,AccountFlowTypeEnum.WITHDRAW) ? BigDecimal.ZERO : amount;
        baseMapper.changeBalance(doctorAccount.getId(),amount,changeTotalAmount);

        //【二】新增医生账户流水（返回添加的整条信息）
        return doctorAccountFlowService.addAccountFlow(doctorAccount.getId(),amount,flowType,relationId);
    }

    @Override
    public void changeBalanceOnly(String doctorId,BigDecimal amount){
        DoctorAccount doctorAccount = getByDoctorId(doctorId);
        if (Objects.isNull(doctorAccount)){
            throw new RuntimeException("未获取到医生账户信息");
        }
        baseMapper.changeBalance(doctorAccount.getId(),amount,amount);
    }



}
